<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="日期" prop="registrationDate">
        <el-date-picker v-model="queryParams.registrationDate" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px" />
      </el-form-item>

      <el-form-item label="车间名" prop="workshopName">
        <el-tree-select v-model="queryParams.workshopName" :data="deptTree" :props="defaultProps" check-strictly
          filterable clearable default-expand-all placeholder="请选择车间" value-key="id" class="!w-240px" />
      </el-form-item>

      <el-form-item label="提出人" prop="proposer">
        <el-select v-model="queryParams.proposer" filterable clearable placeholder="请选择提出人" style="width: 100%;"
          class="!w-240px">
          <el-option v-for="dict in userList" :key="dict.id" :label="dict.nickname" :value="dict.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="停线类型" prop="category">
        <el-select @change="handleQuery" clearable class="!w-240px" v-model="queryParams.category"
          placeholder="请选择设备分类">
          <el-option v-for="dict in category" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="停线问题" prop="question">
        <el-input v-model="queryParams.question" placeholder="请输入停线问题" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="停线原因" prop="reason">
        <el-input v-model="queryParams.reason" placeholder="请输入停线原因" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker v-model="queryParams.startTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px" />
      </el-form-item>
      <!--       <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="queryParams.endTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px" />
      </el-form-item> -->
      <!--       <el-form-item label="持续时间(分钟)" prop="duration">
        <el-input v-model="queryParams.duration" placeholder="请输入持续时间(分钟)" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->
      <!--       <el-form-item label="责任部门" prop="responsibleDepartment">
        <el-input v-model="queryParams.responsibleDepartment" placeholder="请输入责任部门" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->
      <!--       <el-form-item label="责任人" prop="responsiblePerson">
        <el-input v-model="queryParams.responsiblePerson" placeholder="请输入责任人" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->
      <!--       <el-form-item label="影响人数" prop="numberOfAffected">
        <el-input v-model="queryParams.numberOfAffected" placeholder="请输入影响人数" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item> -->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['mold:linestop-registration:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['mold:linestop-registration:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['mold:linestop-registration:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange" border resizable>
      <el-table-column type="selection" width="55" />
      <el-table-column label="日期" align="center" prop="registrationDate" />
      <!--       <el-table-column label="车间名" align="center" prop="workshopName" /> -->
      <el-table-column label="车间名" align="center" prop="workshopName2" />
      <!--     <el-table-column label="提出人" align="center" prop="proposer" /> -->
      <el-table-column label="提出人" align="center" prop="proposerName" />
      <el-table-column label="停线类型" align="center" prop="categoryName" />
      <el-table-column label="停线问题" align="center" prop="question" />
      <el-table-column label="停线原因" align="center" prop="reason" />
      <el-table-column label="开始时间" align="center" prop="startTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="结束时间" align="center" prop="endTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="持续时间(分钟)" align="center" prop="duration" />
      <el-table-column label="责任部门" align="center" prop="responsibleDepartmentName" />
      <el-table-column label="责任人" align="center" prop="respersons" />
      <el-table-column label="影响人数" align="center" prop="numberOfAffected" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" min-width="180px" fixed="right">
        <template #default="scope">
          <!--    <el-button link type="primary" @click="openQuery(scope.row.id)">
            查询
          </el-button> -->
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['mold:linestop-registration:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['mold:linestop-registration:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <LinestopRegistrationForm ref="formRef" @success="getList" />
  <!-- 查询弹窗：只读查看 -->
  <LinestopRegistrationQuery ref="queryRef" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import { defaultProps, handleTree } from '@/utils/tree'
import * as UserApi from '@/api/system/user'
import * as DeptApi from '@/api/system/dept'
import download from '@/utils/download'
import { LinestopRegistrationApi, LinestopRegistration } from '@/api/mold/linestopregistration'
import LinestopRegistrationForm from './LinestopRegistrationForm.vue'
import LinestopRegistrationQuery from './LinestopRegistrationQuery.vue'
import { LinestopCategoryApi } from '@/api/mold/linestopcategory'
import type { UserVO } from '@/api/system/user'
/** 停线登记 列表 */
defineOptions({ name: 'LinestopRegistration' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<LinestopRegistration[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  registrationDate: [],
  workshopName: undefined,
  proposer: undefined,
  category: undefined,
  question: undefined,
  reason: undefined,
  startTime: [],
  endTime: [],
  duration: undefined,
  responsibleDepartment: undefined,
  responsiblePerson: undefined,
  numberOfAffected: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const category = ref([]) // 停线类型

const deptTree = ref() // 树形结构
const userList = ref<UserVO[]>([])



const getCategoryAll = async () => {
  const res = await LinestopCategoryApi.getLinestopCategoryAll();
  category.value = res
}


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LinestopRegistrationApi.getLinestopRegistrationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 获得部门树 */
const getTree = async () => {
  deptTree.value = []
  let parentId = 115
  const data = await DeptApi.getDeptsById(parentId)
  let dept: Tree = { id: parentId, name: '阿能集团', children: [] }
  dept.children = handleTree(data)
  deptTree.value.push(dept)
  //  console.log(deptTree.value)
}


const getUserList = async () => {
  userList.value = await UserApi.getSimpleUserList()
}



/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 查看操作 **/
const queryRef = ref()
const openQuery = (id: number) => {
  queryRef.value.open(id)
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await LinestopRegistrationApi.deleteLinestopRegistration(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除停线登记 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await LinestopRegistrationApi.deleteLinestopRegistrationList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: LinestopRegistration[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await LinestopRegistrationApi.exportLinestopRegistration(queryParams)
    download.excel(data, '停线登记.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getUserList()
  getList()
  getCategoryAll()
  getTree()
})
</script>